package com.zzb.servlet.nwes;


import com.alibaba.fastjson.JSONObject;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.NewsVO;
import com.zzb.service.NewsService;
import com.zzb.service.impl.NewsServiceImpl;
import com.zzb.util.Tool;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/back/news/update")
public class NewsUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //乱码处理
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());

        //得到前端数据
        String id = req.getParameter("id");
        String title = req.getParameter("title");
        String enable = req.getParameter("enable");
        String isShow = req.getParameter("isShow");
        String content = req.getParameter("content");
        String imgHref = req.getParameter("imgHref");
        String isRecommend = req.getParameter("isRecommend");
        String typeId = req.getParameter("typeId");


        //对前端数据进行封装
        NewsVO newsVO = new NewsVO();
        newsVO.setId(Integer.valueOf(id));
        newsVO.setTitle(Tool.nullToStr(title));
        newsVO.setContent(Tool.nullToStr(content));
        newsVO.setImgHref(Tool.nullToStr(imgHref));
        newsVO.setIsShow(Integer.valueOf(Tool.nullToNum(isShow)));
        newsVO.setTypeId(Integer.valueOf(Tool.nullToNum(typeId)));
        newsVO.setEnable(Integer.valueOf(Tool.nullToNum(enable)));
        newsVO.setIsRecommend(Integer.valueOf(Tool.nullToNum(isRecommend)));
        //调用服务逻辑
        NewsService newsService = new NewsServiceImpl();

        //得到返回值
        ResultDTO resultDTO = newsService.update(newsVO);
        //将数据响应给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
